package com.zero.lite.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zero.lite.dao.entity.Address;
import com.zero.lite.dao.service.IAddressService;
import com.zero.lite.dao.service.IRegionAreaService;
import com.zero.lite.dao.utils.BaseController;
import com.zero.lite.dao.utils.ResponseUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.zero.lite.admin.annotation.LoginAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/address")
public class AddressController extends BaseController {

    @Autowired
    private IAddressService addressService;
    @Autowired
    private IRegionAreaService regionService;

    private Map<String, Object> toVo (Address address){
        Map<String, Object> addressVo = new HashMap<>();
        addressVo.put("id", address.getId());
        addressVo.put("userId", address.getUserId());
        addressVo.put("name", address.getName());
        addressVo.put("mobile", address.getMobile());
        addressVo.put("isDefault", address.getIsDefault());
        addressVo.put("provinceId", address.getProvinceId());
        addressVo.put("cityId", address.getCityId());
        addressVo.put("areaId", address.getAreaId());
        addressVo.put("address", address.getAddress());
        String province = regionService.regionName(address.getProvinceId());
        String city = regionService.regionName(address.getCityId());
        String area = regionService.regionName(address.getAreaId());
        addressVo.put("province", province);
        addressVo.put("city", city);
        addressVo.put("area", area);
        return addressVo;
    }

    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       Integer userId, String name,
                       Page page){
        if(adminId == null){
            return ResponseUtil.fail401();
        }

        EntityWrapper<Address> ew=new EntityWrapper();
        ew.eq("user_id",userId).eq("name",name);
        Page<Address> data=addressService.selectPage(page,ew);

        List<Map<String, Object>> addressVoList = new ArrayList<>(data.getRecords().size());
        for(Address address : data.getRecords()){
            Map<String, Object> addressVo = toVo(address);
            addressVoList.add(addressVo);
        }
        return ResponseUtil.PageOk(data);
    }

    @PostMapping("/create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody Address address){
        if(adminId == null){
            return ResponseUtil.fail401();
        }

        addressService.insert(address);

        Map<String, Object> addressVo = toVo(address);
        return ResponseUtil.ok(addressVo);
    }

    @GetMapping("/read")
    public Object read(@LoginAdmin Integer adminId, Integer addressId){
        if(adminId == null){
            return ResponseUtil.fail401();
        }

        Address address = addressService.selectById(addressId);
        Map<String, Object> addressVo = toVo(address);
        return ResponseUtil.ok(addressVo);
    }

    @PostMapping("/update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody Address address){
        if(adminId == null){
            return ResponseUtil.fail401();
        }
        addressService.updateById(address);
        Map<String, Object> addressVo = toVo(address);
        return ResponseUtil.ok(addressVo);
    }

    @PostMapping("/delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody Address address){
        if(adminId == null){
            return ResponseUtil.fail401();
        }
        addressService.deleteById(address.getId());
        return ResponseUtil.ok();
    }

}
